package com.ruoyi.mall.controller;

import java.text.AttributedString;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import com.ruoyi.mall.entity.MallCategory;
import com.ruoyi.mall.entity.MallProperty;
import com.ruoyi.mall.front.vo.MallMembersVo;
import com.ruoyi.mall.service.IMallCategoryService;
import com.ruoyi.mall.service.IMallPropertyService;
import com.ruoyi.mall.vo.MallProductVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.entity.MallProduct;
import com.ruoyi.mall.service.IMallProductService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品Controller
 * 
 * @author ruoyi
 * @date 2020-05-20
 */
@Controller
@RequestMapping("/mall/product")
public class MallProductController extends BaseController
{
    private String prefix = "mall/product";

    @Autowired
    private IMallProductService mallProductService;
    @Autowired
    private IMallCategoryService mallGoodCategoryService;
    @Autowired
    private IMallPropertyService mallPropertyService;

    @RequiresPermissions("mall:product:view")
    @GetMapping()
    public String product(ModelMap mmap)
    {
        mmap.addAttribute("categorys",getCategory());
        return prefix + "/product";
    }

    /**
     * 查询产品列表
     */
    @RequiresPermissions("mall:product:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MallProduct mallProduct)
    {
        startPage();
        List<MallProduct> list = mallProductService.selectMallProductList(mallProduct);
        return getDataTable(list);
    }

    /**
     * 导出产品列表
     */
    @RequiresPermissions("mall:product:export")
    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MallProduct mallProduct)
    {
        List<MallProduct> list = mallProductService.selectMallProductList(mallProduct);
        ExcelUtil<MallProduct> util = new ExcelUtil<MallProduct>(MallProduct.class);
        return util.exportExcel(list, "product");
    }

    /**
     * 新增产品
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    { mmap.addAttribute("categorys",getCategory());
        return prefix + "/add";
    }

    /**
     * 新增保存产品
     */
    @RequiresPermissions("mall:product:add")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MallProductVo mallProduct)
    {
        List<MallProperty> mallProperties = mallPropertyService.selectMallPropertyByIds(mallProduct.getProperty());
        mallProduct.setPropertys(JSON.toJSONString(mallProperties));
        return toAjax(mallProductService.insertMallProduct(mallProduct));
    }

    /**
     * 获取所有分类
     * @return
     */
    public List<MallCategory> getCategory(){
        return mallGoodCategoryService.selectMallCategoryList(null);
    }
    /**
     * 修改产品
     */
    @GetMapping("/edit/{productId}")
    public String edit(@PathVariable("productId") Long productId, ModelMap mmap)
    {
        MallProduct mallProduct = mallProductService.selectMallProductById(productId);
        List<MallProperty> mallProperties = JSON.parseArray(mallProduct.getPropertys(), MallProperty.class);
        List<Long> collect = mallProperties.stream().map(property -> property.getPropertyId())
                .collect(Collectors.toList());
        mmap.put("mallProduct", mallProduct);
        mmap.put("propertys", JSON.toJSON(collect));
        mmap.addAttribute("categorys",getCategory());
        return prefix + "/edit";
    }

    /**
     * 修改保存产品
     */
    @RequiresPermissions("mall:product:edit")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MallProductVo mallProduct)
    {
        List<MallProperty> mallProperties = mallPropertyService.selectMallPropertyByIds(mallProduct.getProperty());
        mallProduct.setPropertys(JSON.toJSONString(mallProperties));
        return toAjax(mallProductService.updateMallProduct(mallProduct));
    }

    /**
     * 删除产品
     */
    @RequiresPermissions("mall:product:remove")
    @Log(title = "产品", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mallProductService.deleteMallProductByIds(ids));
    }
}
